<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>案例_用户登录.html</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
  <style>
    html,
    body {
      background-color: #f8f8f8;
    }

    .login-box {
      width: 400px;
      position: fixed;
      top: 20%;
      left: 50%;
      transform: translateX(-50%);
      border: 1px solid #efefef;
      padding: 20px;
      border-radius: 4px;
      box-shadow: 1px 1px 5px #cfcfcf;
      background-color: #fff;
      transition: box-shadow 0.3s ease;
    }

    .login-box:hover {
      transition: box-shadow 0.3s ease;
      box-shadow: 1px 1px 20px #cfcfcf;
    }
  </style>
</head>

<body>
  <div class="login-box">
    <div class="form-group">
      <label for="username">Account</label>
      <!-- 账号 -->
      <input type="text" class="form-control" id="username" autocomplete="off">
      <small id="emailHelp" class="form-text text-muted">
        The available account is<strong>admin</strong>
      </small>
    </div>
    <div class="form-group">
      <!-- 密码 -->
      <label for="password">Password</label>
      <input type="password" class="form-control" id="password">
      <small id="emailHelp" class="form-text text-muted">The available password is
        <strong>123456</strong></small>

    </div>
    <button type="submit" class="btn btn-primary" id="btnLogin">Submit</button>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/axios@0.27.2/dist/axios.min.js"></script>

  <script>
    // 给按钮添加点击事件
    const btn = document.querySelector('#btnLogin')
    btn.addEventListener('click', () => {
        // alert(1)
        // 获取输入框的值
        const username = document.querySelector('#username').value.trim()
        const password = document.querySelector('#password').value.trim()
        // alert(password)
        axios({
            url: 'http://ajax-api.itheima.net/api/login',
            method: 'POST',
            data: {
                username: username,
                password: password
            }
        }).then(res => {
            console.log(res)
            alert('登录成功')
        }).catch(err => {
            alert('登录失败')
            console.log(err)
        })

        // 如果请求是正常的，就会进入 then
        // 如果请求是异常的，就会进入 catch
    })
  </script>
</body>

</html>